#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

# do not strip SOs
export DONTSTRIP=1
#export DEB_BUILD_OPTIONS="nostrip"

# see FEATURE AREAS in dpkg-buildflags(1)
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# see ENVIRONMENT in dpkg-buildflags(1)
# package maintainers to append CFLAGS
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# package maintainers to append LDFLAGS
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

VERSION=${RAVENDB_VERSION}
PACKAGE_REVISION=${PACKAGE_REVISION:-0}
PACKAGEVERSION="${VERSION}-${PACKAGE_REVISION}~${DISTRIBUTION}0"
DISTRIBUTION=${DISTRO_VERSION_NAME}
TARBALL_UNPACK_DIR=/tmp
TARBALL_UNPACK_RAVENDB_DIR=${TARBALL_UNPACK_DIR}/RavenDB
TARBALL_UNPACK_SERVER_DIR=${TARBALL_UNPACK_RAVENDB_DIR}/Server
ETC_ASSETS_DIR=$(BUILD_DIR)/ravendb/etc

mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST)))

%:
	dh $@ --with systemd

override_dh_shlibdeps:
override_dh_auto_clean:
override_dh_auto_test:
override_dh_auto_build:
override_dh_auto_install: prepare_from_tarball 
	mkdir -p debian/ravendb/usr/lib
	mv -v ${TARBALL_UNPACK_RAVENDB_DIR} debian/ravendb/usr/lib/ravendb
	$(MAKE) -f $(mkfile_path) -C debian/ravendb make_etc_layout

make_etc_layout:
	mkdir -p etc/ld.so.conf.d
	cp -rv $(ETC_ASSETS_DIR)/ld.so.conf.d/ravendb.conf etc/ld.so.conf.d/ravendb.conf

adjust_tarball_ravendb_dir:
	mv Server server
	rm -v *.sh

adjust_tarball_server_dir:
	cp -v librvnpal.${RAVEN_SO_ARCH_SUFFIX}.so librvnpal.so
	cp -v libzstd.${RAVEN_SO_ARCH_SUFFIX}.so libzstd.so

	find . -maxdepth 1 -type f -name "librvnpal.*.so" ! -iname "*.${RAVEN_SO_ARCH_SUFFIX}.so" -exec rm -v {} \;
	find . -maxdepth 1 -type f -name "libzstd.*.so" ! -iname "*.${RAVEN_SO_ARCH_SUFFIX}.so" -exec rm -v {} \;

	cp -rv $(ETC_ASSETS_DIR)/ravendb/settings.json settings.default.json
	mkdir -p libmscordaccore


prepare_from_tarball:
	tar -C ${TARBALL_UNPACK_DIR} -xf ${CACHED_TARBALL}
	$(MAKE) -f $(mkfile_path) -C ${TARBALL_UNPACK_SERVER_DIR} adjust_tarball_server_dir	
	$(MAKE) -f $(mkfile_path) -C ${TARBALL_UNPACK_RAVENDB_DIR} adjust_tarball_ravendb_dir	

override_dh_fixperms:
	dh_fixperms	

	find debian/ravendb/usr/lib/ravendb -type f -exec chmod 644 {} +

	chmod 755 debian/ravendb/usr/lib/ravendb/server/Raven.Server
	chmod 755 debian/ravendb/usr/lib/ravendb/server/rvn
	chmod 750 debian/ravendb/usr/lib/ravendb/server/Raven.Debug
	chmod 750 debian/ravendb/usr/lib/ravendb/server/createdump

